package whu.o2o.util;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import whu.o2o.vo.BaseVO;
import whu.o2o.vo.BaseinfoVO;
import whu.o2o.vo.CityVO;
import whu.o2o.vo.DiaryVO;
import whu.o2o.vo.HistoryBookVO;
import whu.o2o.vo.PersonVO;
import whu.o2o.vo.RouteVO;
import whu.o2o.vo.StudioVO;
import whu.o2o.vo.WonderfulDiaryVO;
import android.text.TextUtils;
import android.util.Log;






public class ParseXml {

	private static ParseXml instance;

	private ParseXml() {

	}

	public static synchronized ParseXml getInstance() {
		if (instance == null) {
			instance = new ParseXml();
		}
		return instance;
	}

	private String getValue(Element rootElement, String nodeName) {
		Element element = (Element) rootElement.getElementsByTagName(nodeName).item(0);
		if (element != null && element.getChildNodes().getLength() > 0) {
			return element.getFirstChild().getNodeValue();
		}
		return "";
	}

	private Element getElement(Element rootElement, String nodeName) {
		Element element = (Element) rootElement.getElementsByTagName(nodeName).item(0);
		if (element != null && element.getChildNodes().getLength() > 0) {
			return element;
		}
		return null;
	}

	/**
	 * 解析旅游路线
	 */
	public void parseRouteXml(String xml, ArrayList<RouteVO> list) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			String page = getValue(root, "page");

			NodeList routeNodeList = root.getElementsByTagName("route");
			for (int i = 0; i < routeNodeList.getLength(); i++) {
				Element routeElement = (Element) routeNodeList.item(i);

				String id = getValue(routeElement, "id");
				String title = getValue(routeElement, "title");
				String introduction = getValue(routeElement, "introduction");
				String price = getValue(routeElement, "price");
				String reserved = getValue(routeElement, "reserved");
				String image = getValue(routeElement, "image");

				RouteVO routeVO = new RouteVO();
				routeVO.setId(Utils.str2int(id));
				routeVO.setName(title);
				routeVO.setIntroduction(introduction);
				routeVO.setPrice(Utils.str2int(price));
				routeVO.setReservedCount(Utils.str2int(reserved));
				routeVO.setImage(image);
				routeVO.setPage(Utils.str2int(page));

				list.add(routeVO);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析路线详情
	 */
	public void parseRouteDetailXml(String xml, RouteVO routeVO) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			String startTime = getValue(root, "start");
			String goodCount = getValue(root, "good");
			String commentCount = getValue(root, "comment");
			String reserved = getValue(root, "reserved");
			String detail = getValue(root, "details");
			String feature = getValue(root, "feature");
			String tip = getValue(root, "tip");
			String traffic = getValue(root, "traffic");
			String priceDetail = getValue(root, "price_details");

			PersonVO personVO = new PersonVO();

			routeVO.setStartTime(startTime);
			routeVO.setGoodCount(Utils.str2int(goodCount));
			routeVO.setCommentCount(Utils.str2int(commentCount));
			routeVO.setReserved(ConstantData.Data.RESERVED == Utils.str2int(reserved));
			routeVO.setDetail(detail);
			routeVO.setFeature(feature);
			routeVO.setTip(tip);
			routeVO.setTraffic(traffic);
			routeVO.setPriceDetail(priceDetail);
			routeVO.setGuideInfo(personVO);

			Element guideElement = getElement(root, "guide");
			if (guideElement != null) {
				String guideId = getValue(guideElement, "id");
				String guideName = getValue(guideElement, "name");
				String guideSex = getValue(guideElement, "sex");
				String guideHeadUrl = getValue(guideElement, "head");
				String guideAge = getValue(guideElement, "age");
				String guideLevel = getValue(guideElement, "grade");
				String guideState = getValue(guideElement, "authentication");
				String guidePhone = getValue(guideElement, "phone");

				personVO.setId(Utils.str2int(guideId));
				personVO.setName(guideName);
				personVO.setSex(Utils.str2int(guideSex));
				personVO.setAge(Utils.str2int(guideAge));
				personVO.setHeadUrl(guideHeadUrl);
				personVO.setLevel(Utils.str2int(guideLevel));
				personVO.setValidated(ConstantData.Data.VALIDATED == Utils.str2int(guideState));
				personVO.setPhone(guidePhone);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析城市列表
	 * 
	 * @param xml
	 * @param list
	 */
	public void parseCityListXml(String xml, ArrayList<CityVO> list) {
		try {
			String letter = "";

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			NodeList cityNodeList = root.getElementsByTagName("city");
			for (int i = 0; i < cityNodeList.getLength(); i++) {
				Element cityElement = (Element) cityNodeList.item(i);

				String name = getValue(cityElement, "name");
				String pinyin = getValue(cityElement, "pinyin");

				String first = pinyin.substring(0, 1);
				if (!letter.equals(first)) {
					CityVO cityVO = new CityVO();
					cityVO.setName(first.toUpperCase()); // 转换为大写
					cityVO.setPinyin(first);
					cityVO.setType(ConstantData.Data.CITYLIST_LETTER_TYPE);
					list.add(cityVO);

					letter = first;
				}

				CityVO cityVO = new CityVO();
				cityVO.setName(name);
				cityVO.setPinyin(pinyin);
				cityVO.setType(ConstantData.Data.CITYLIST_CITY_TYPE);
				list.add(cityVO);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析目的地分组列表
	 */
	public void parseDestinationGroupXml(String xml, ArrayList<BaseVO> list) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			NodeList groupNodeList = root.getElementsByTagName("destinationgroup");
			for (int i = 0; i < groupNodeList.getLength(); i++) {
				Element groupElement = (Element) groupNodeList.item(i);

				String id = getValue(groupElement, "id");
				String name = getValue(groupElement, "name");

				BaseVO baseVO = new BaseVO();
				baseVO.setId(Utils.str2int(id));
				baseVO.setName(name);

				list.add(baseVO);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析目的地分组列表
	 */
	public void parseDestinationListXml(String xml, ArrayList<Map<String, Object>> list) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			NodeList destinationNodeList = root.getElementsByTagName("destination");
			for (int i = 0; i < destinationNodeList.getLength(); i++) {
				Element destinationElement = (Element) destinationNodeList.item(i);
				String name = getValue(destinationElement, "name");

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("name", name);
				list.add(map);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析人物信息
	 * 
	 * @param xml
	 * @param list
	 */
	public void parsePersonXml(String xml, ArrayList<PersonVO> list) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			String page = getValue(root, "page");

			NodeList guideNodeList = root.getElementsByTagName("guide");
			for (int i = 0; i < guideNodeList.getLength(); i++) {
				Element guideElement = (Element) guideNodeList.item(i);

				String id = getValue(guideElement, "id");
				String name = getValue(guideElement, "name");
				String sex = getValue(guideElement, "sex");
				String head = getValue(guideElement, "head");
				String age = getValue(guideElement, "age");
				String level = getValue(guideElement, "grade");
				String validated = getValue(guideElement, "authentication");
				String cardImage = getValue(guideElement, "card_image");
				String introduction = getValue(guideElement, "introduction");

				PersonVO personVO = new PersonVO();
				personVO.setId(Utils.str2int(id));
				personVO.setName(name);
				personVO.setSex(Utils.str2int(sex));
				personVO.setHeadUrl(head);
				personVO.setAge(Utils.str2int(age));
				personVO.setLevel(Utils.str2int(level));
				personVO.setValidated(ConstantData.Data.VALIDATED == Utils.str2int(validated));
				personVO.setCardImageUrl(cardImage);
				personVO.setIntroduction(introduction);
				personVO.setPage(Utils.str2int(page));

				list.add(personVO);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析旅行社
	 * 
	 * @param xml
	 * @param list
	 */
	public void parseAgencyXml(String xml, ArrayList<BaseVO> list) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			NodeList agencyNodeList = root.getElementsByTagName("agency");
			for (int i = 0; i < agencyNodeList.getLength(); i++) {
				Element agencyElement = (Element) agencyNodeList.item(i);

				String id = getValue(agencyElement, "id");
				String name = getValue(agencyElement, "name");

				BaseVO baseVO = new BaseVO();
				baseVO.setId(Utils.str2int(id));
				baseVO.setName(name);

				list.add(baseVO);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析日记
	 * 
	 * @param xml
	 * @param list
	 */
	public void parseDiaryXml(String xml, ArrayList<DiaryVO> list) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			NodeList agencyNodeList = root.getElementsByTagName("agency");
			for (int i = 0; i < agencyNodeList.getLength(); i++) {
				Element agencyElement = (Element) agencyNodeList.item(i);

				String id = getValue(agencyElement, "id");
				String name = getValue(agencyElement, "name");

				DiaryVO diaryVO = new DiaryVO();

				list.add(diaryVO);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析工作室列表
	 */
	public void parseStudioXml(String xml, ArrayList<StudioVO> list) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			NodeList studioNodeList = root.getElementsByTagName("studio");
			for (int i = 0; i < studioNodeList.getLength(); i++) {
				Element studioElement = (Element) studioNodeList.item(i);

				String id = getValue(studioElement, "id");
				String logoUrl = getValue(studioElement, "logo");
				String name = getValue(studioElement, "name");
				String peopleCount = getValue(studioElement, "people");
				String focusedCount = getValue(studioElement, "focus");
				String focused = getValue(studioElement, "focused");
				String introduction = getValue(studioElement, "introduction");
				String label = getValue(studioElement, "label");

				StudioVO studioVO = new StudioVO();
				studioVO.setId(Utils.str2int(id));
				studioVO.setLogoUrl(logoUrl);
				studioVO.setName(name);
				studioVO.setPeopleCount(Utils.str2int(peopleCount));
				studioVO.setFocusedCount(Utils.str2int(focusedCount));
				studioVO.setFocused(ConstantData.Data.FOCUSED == Utils.str2int(focused));
				studioVO.setIntroduction(introduction);
				studioVO.setLabel(label);
				list.add(studioVO);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析导游名片
	 * 
	 * @param xml
	 * @param list
	 */
	public void parseGuideCardXml(String xml, PersonVO guideVO) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			String name = getValue(root, "name");
			String sex = getValue(root, "sex");
			String head = getValue(root, "icon");
			String age = getValue(root, "age");
			String level = getValue(root, "level");
			String validated = getValue(root, "auth");
			String cardImage = getValue(root, "cardimg");
			String introduction = getValue(root, "remark");
			String goodCount = getValue(root, "zancount");
			String commentCount = getValue(root, "pingluncount");
			String receptionType = getValue(root, "undertake");
			String looks = getValue(root, "bueaty");
			String humor = getValue(root, "humor");
			String profession = getValue(root, "major");
			String patience = getValue(root, "patience");
			String erudite = getValue(root, "erudite");
			String specialty = getValue(root, "zhuanyetechang");
			String guideCard = getValue(root, "guidecode");
			String qualificationCard = getValue(root, "qualcode");
			String idCard = getValue(root, "card");
			String phone = getValue(root, "mobil");
			String agency = getValue(root, "travel");
			String language = getValue(root, "languages");

			guideVO.setName(name);
			guideVO.setSex(Utils.str2int(sex));
			guideVO.setHeadUrl(head);
			guideVO.setAge(Utils.str2int(age));
			guideVO.setLevel(Utils.str2int(level));
			guideVO.setValidated(ConstantData.Data.VALIDATED == Utils.str2int(validated));
			guideVO.setCardImageUrl(cardImage);
			guideVO.setIntroduction(introduction);
			guideVO.setGoodCount(Utils.str2int(goodCount));
			guideVO.setCommentCount(Utils.str2int(commentCount));
			guideVO.setReceptionType(Utils.str2int(receptionType));
			guideVO.setLooks(Utils.str2int(looks));
			guideVO.setHumor(Utils.str2int(humor));
			guideVO.setProfession(Utils.str2int(profession));
			guideVO.setPatience(Utils.str2int(patience));
			guideVO.setErudite(Utils.str2int(erudite));
			guideVO.setSpecialty(specialty);
			guideVO.setGuideCard(guideCard);
			guideVO.setQualificationCard(qualificationCard);
			guideVO.setIdCard(idCard);
			guideVO.setPhone(phone);
			guideVO.setAgency(agency);
			guideVO.setLanguage(language);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析达人名片
	 * 
	 * @param xml
	 * @param list
	 */
	public void parseMasterCardXml(String xml, PersonVO masterVO) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			String name = getValue(root, "name");
			String sex = getValue(root, "sex");
			String head = getValue(root, "icon");
			String age = getValue(root, "age");
			String level = getValue(root, "level");
			String cardImage = getValue(root, "cardimg");
			String goodCount = getValue(root, "zancount");
			String commentCount = getValue(root, "pingluncount");
			String looks = getValue(root, "bueaty");
			String humor = getValue(root, "humor");
			String profession = getValue(root, "major");
			String patience = getValue(root, "patience");
			String erudite = getValue(root, "erudite");
			String idCard = getValue(root, "card");
			String phone = getValue(root, "mobil");

			String serviceItem = getValue(root, "phone");
			String serviceContent = getValue(root, "content");
			String refundDetail = getValue(root, "refund");

			masterVO.setName(name);
			masterVO.setSex(Utils.str2int(sex));
			masterVO.setHeadUrl(head);
			masterVO.setAge(Utils.str2int(age));
			masterVO.setLevel(Utils.str2int(level));
			masterVO.setCardImageUrl(cardImage);
			masterVO.setGoodCount(Utils.str2int(goodCount));
			masterVO.setCommentCount(Utils.str2int(commentCount));
			masterVO.setLooks(Utils.str2int(looks));
			masterVO.setHumor(Utils.str2int(humor));
			masterVO.setProfession(Utils.str2int(profession));
			masterVO.setPatience(Utils.str2int(patience));
			masterVO.setErudite(Utils.str2int(erudite));
			masterVO.setIdCard(idCard);
			masterVO.setPhone(phone);
			masterVO.setServiceItem(serviceItem);
			masterVO.setServiceContent(serviceContent);
			masterVO.setRefundDetail(refundDetail);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析游客名片
	 * 
	 * @param xml
	 * @param list
	 */
	public void parseGuestCardXml(String xml, PersonVO guestVO) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			String name = getValue(root, "name");
			String sex = getValue(root, "sex");
			String head = getValue(root, "icon");
			String age = getValue(root, "age");
			String level = getValue(root, "level");
			String cardImage = getValue(root, "cardimg");
			String city = getValue(root, "city");
			String introduction = getValue(root, "remark");
			Log.d("11", city);

			ArrayList<WonderfulDiaryVO> wonderfulDiaryList = new ArrayList<WonderfulDiaryVO>();
			Element diaryListElement = getElement(root, "diary_list");
			if (diaryListElement != null) {
				NodeList diaryNodeList = root.getElementsByTagName("diary");
				for (int i = 0; i < diaryNodeList.getLength(); i++) {
					Element diaryElement = (Element) diaryNodeList.item(i);

					String title = getValue(diaryElement, "title");
					String userName = getValue(diaryElement, "user_name");
					String time = getValue(diaryElement, "time");
					String imageUrl = getValue(diaryElement, "image");

					WonderfulDiaryVO diaryVO = new WonderfulDiaryVO();
					diaryVO.setTitle(title);
					diaryVO.setUserName(userName);
					diaryVO.setTime(time);
					diaryVO.setImageUrl(imageUrl);
					wonderfulDiaryList.add(diaryVO);
				}
			}

			guestVO.setName(name);
			guestVO.setSex(Utils.str2int(sex));
			guestVO.setHeadUrl(head);
			guestVO.setAge(Utils.str2int(age));
			guestVO.setLevel(Utils.str2int(level));
			guestVO.setCardImageUrl(cardImage);
			guestVO.setIdCard(city);
			guestVO.setIntroduction(introduction);
			guestVO.setWonderfulDiaryList(wonderfulDiaryList);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 解析登录信息
	 * 
	 * @param xml
	 * @param list
	 */
	public void parseLoginInfoXml(String xml, PersonVO userVO) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			String type = getValue(root, "type");
			String userId = getValue(root, "userid");
			String name = getValue(root, "name");
			String phone = getValue(root, "mobile");
			String age = getValue(root, "age");
			String headUrl = getValue(root, "icon");
			String sex = getValue(root, "sex");
			String level = getValue(root, "level");
			String validated = getValue(root, "auth");
			String cardImageUrl = getValue(root, "cardimg");

			userVO.setType(Utils.str2int(type));
			userVO.setId(Utils.str2int(userId));
			userVO.setName(name);
			userVO.setSex(Utils.str2int(sex));
			userVO.setHeadUrl(headUrl);
			userVO.setCardImageUrl(cardImageUrl);
			userVO.setAge(Utils.str2int(age));
			userVO.setLevel(Utils.str2int(level));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 解析历史抢单接口
	 * 
	 * @param xml
	 * @param list
	 */
	
	public void parseHistroyBookXml(String xml, ArrayList<HistoryBookVO> list) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			ByteArrayInputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8"));
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dom = db.parse(buf);
			Element root = dom.getDocumentElement();

			NodeList studioNodeList = root.getElementsByTagName("call");
			for (int i = 0; i < studioNodeList.getLength(); i++) {
				Element studioElement = (Element) studioNodeList.item(i);

				String id = getValue(studioElement, "id");
				String userid = getValue(studioElement, "userid");
				String name = getValue(studioElement, "name");
				String icon = getValue(studioElement, "icon");
				String age = getValue(studioElement, "age");
				String sex = getValue(studioElement, "sex");
				String voice = getValue(studioElement, "voice");
				String enddate = getValue(studioElement, "enddate");
				String remark = getValue(studioElement, "remark");
				String state = getValue(studioElement, "state");
				

				HistoryBookVO studioVO = new HistoryBookVO();
				studioVO.setId(id);
				studioVO.setUserid(userid);
				studioVO.setName(name);
				studioVO.setIcon(icon);
				studioVO.setAge(age);
				studioVO.setSex(sex);
				studioVO.setVoice(voice);
				studioVO.setEnddate(enddate);
				studioVO.setRemark(remark);
				studioVO.setState(state);
				if (!TextUtils.isEmpty(voice) && voice.contains("/")) {
					String fileName = voice.substring(voice.lastIndexOf("/") + 1);
					studioVO.setFilename(fileName);
					studioVO.setFilepath(Utils.AUDIO_PATH + fileName);
				} else {
					studioVO.setFilename("");
				}
				list.add(studioVO);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	

}
